﻿using NalogGarLoader.Data;
using System.Diagnostics;
using System.Reflection;

namespace NalogGarLoader;

internal class Program
{
    private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();

    static void Main(string[] args)
    {
        try
        {
            log.Trace("Main start");
            
            Console.WriteLine(Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyTitleAttribute>()!.Title);    
            Console.WriteLine(Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyDescriptionAttribute>()!.Description);        
            Console.WriteLine($"Version: {Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()!.InformationalVersion}");
            Console.WriteLine($"Author: {Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyCompanyAttribute>()!.Company}");
            Console.WriteLine();

            if (args.Where(arg => (new[] {"-h", "-help", "/?", "--help" }).Contains(arg.ToLower())).Any())
            {
                GarImportParameters.PrintConsoleHelp();
                return;
            }

            GarImportParameters importParameters = new();            

            string jsonSettingsFile = Path.Combine(Environment.CurrentDirectory, "NalogGarLoader.json");

            if (File.Exists(jsonSettingsFile))
                importParameters.InitFromJson(jsonSettingsFile);

            importParameters.InitFromArgs(args);

            Stopwatch stopwatch = Stopwatch.StartNew();

            GarImportController garImportController = new(importParameters);
            garImportController.RunImport();        

            Stopwatch totalImportStopWatch = Stopwatch.StartNew();        

            stopwatch.Stop();

            log.Info($"Общее время работы {stopwatch.Elapsed.ToString("hh\\:mm\\:ss")}");
            log.Trace("Main end");
        }
        catch (Exception exception)
        {
            log.Fatal(exception, "Stopped program because of exception");
            throw;
        }
        finally
        {
            NLog.LogManager.Shutdown();
        }
    }
}